package com.miracle9.game.service;

import com.miracle9.game.dao.VersionServiceDao;
import com.miracle9.game.entity.Version;
import org.apache.mina.core.session.IoSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service("versionService")
public class VersionService {

    @Autowired
    private VersionServiceDao versionServiceDao;

    public Map<String, Object> checkVersion(String versionCode, IoSession session) {
        Map<String, Object> result = new HashMap<>();
        Version version = this.versionServiceDao.getByType(0);
        if ((version == null) || (version.getVersionCode().equals(versionCode))) {
            result.put("haveNewVersionIDFlag", Boolean.FALSE);
            result.put("androidVersion", versionCode);
            result.put("iOSVersion", versionCode);
            return result;
        }
        result.put("haveNewVersionIDFlag", Boolean.TRUE);
        result.put("versionCode", version.getVersionCode());
        result.put("downloadWindows", version.getDownloadWindows());
        result.put("downloadAndroid", version.getDownloadAndroid());
        result.put("androidVersion", version.getVersionCode());
        result.put("iOSVersion", version.getVersionCode());
        return result;
    }

    public Map<String, Object> checkVersion(String versionCode, int type, IoSession session) {
        Map<String, Object> result = new HashMap<>();
        Version version = this.versionServiceDao.getByType(type);
        if ((version == null) || (version.getVersionCode().equals(versionCode))) {
            result.put("haveNewVersionIDFlag", Boolean.FALSE);
            result.put("androidVersion", versionCode);
            result.put("iOSVersion", versionCode);
            return result;
        }
        result.put("haveNewVersionIDFlag", Boolean.TRUE);
        result.put("versionCode", version.getVersionCode());
        result.put("downloadWindows", version.getDownloadWindows());
        result.put("downloadAndroid", version.getDownloadAndroid());
        result.put("androidVersion", version.getVersionCode());
        result.put("iOSVersion", version.getVersionCode());
        return result;
    }

}
